﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Diagnostics;

namespace MwTUpdater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void checkConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OdbcConnection connection = new OdbcConnection("DSN=WatchMyToon_DB");
                connection.Open();
                MessageBox.Show("Connection OK", "Info");
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection not working please create!", "Error");
            }
         
        }

        
        private bool CheckForTables()
        {
            try
            {
                OdbcConnection connection = new OdbcConnection("DSN=WatchMyToon_DB");
                connection.Open();
                OdbcCommand check = connection.CreateCommand();

                check.CommandText = "Select * from `TABMessageReceive`;";

                check.ExecuteNonQuery();

                connection.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            
     
        }

        private void createTablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OdbcConnection connection = new OdbcConnection("DSN=WatchMyToon_DB");
                connection.Open();

                if (!CheckForTables())
                {

                    try
                    {

                        OdbcCommand Create = connection.CreateCommand();
                        string[] lines = System.IO.File.ReadAllLines(Application.StartupPath + "\\tables\\create.sql");
                        foreach (string line in lines)
                        {
                            Create.CommandText = line;
                            Create.ExecuteNonQuery();
                        }

                        MessageBox.Show("Tables Created", "Info");
                        return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error");
                    }


                }
                else
                {
                    MessageBox.Show("Tables already exists!", "Info");
                }


                connection.Close();

                MessageBox.Show("Connection not working please create!", "Error");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection not working please create!", "Error");
            }
        }



        private bool CreateDSN()
        {
            DataConn conn = new DataConn();
            conn.ShowDialog();
            string[] data = conn.ConnectionData;
            conn.Close();

            if (data.Length == 0)
                return false;


            ODBCManager.CreateDSN("WatchMyToon_DB", "DSN for WatchMyToon", data[0], data[1], data[2], "MySQL ODBC 3.51 Driver", true, data[3]);

            return true;
        }


        private void createDSNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                if (ODBCManager.DSNExists("WatchMyToon_DB"))
                {
                    MessageBox.Show("WatchMyToon_DB already Exists! Cancel creation", "Info");
                }

                string[] installed = ODBCManager.GetInstalledDrivers();
                bool bfound = false;


                for (int i = 0; i < installed.Length; i++)
                {
                    if (installed[i] == "MySQL ODBC 3.51 Driver")
                    {
                        bfound = true;
                    }
                }


                if (bfound)
                {
                    if (CreateDSN())
                    {
                        MessageBox.Show("WatchMyToon_DB created", "Info");
                    }
                    else
                    {
                        MessageBox.Show("Creation aborted", "Info");
                    }
                }
                else
                {
                    DialogResult result = MessageBox.Show("Driver \"MySQL ODBC 3.51 Driver\" not installed! Should i run installer?",
                                    "Information",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question);

                    if (result == DialogResult.Yes)
                    {
                        try
                        {
                            ProcessStartInfo startInfo = new ProcessStartInfo();
                            startInfo.FileName = Application.StartupPath + "\\driver\\driver.msi";
                            using (Process exeProcess = Process.Start(startInfo))
                            {
                                exeProcess.WaitForExit();
                            }

                            installed = ODBCManager.GetInstalledDrivers();
                            bfound = false;


                            for (int i = 0; i < installed.Length; i++)
                            {
                                if (installed[i] == "MySQL ODBC 3.51 Driver")
                                {
                                    bfound = true;
                                }
                            }

                            if (bfound)
                            {

                                if (CreateDSN())
                                {

                                    MessageBox.Show("WatchMyToon_DB created", "Info");
                                }
                                else
                                {
                                    MessageBox.Show("Creation aborted", "Info");
                                }
                            }
                            else
                            {
                                MessageBox.Show("Error while install driver", "Error");
                            }

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error");
                        }



                    }
                    else
                    {
                        MessageBox.Show("Creation aborted", "Info");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            
        }

        private void dropDSNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ODBCManager.RemoveDSN("WatchMyToon_DB");
            MessageBox.Show("WatchMyToon_DB dropped", "Info");

        }

       
      
    }
}
